﻿/**
 * 基础计算方法
 */
class calculator_nomal implements ICalculator {
    public getRandomInt(param: { min: number; max: number }) {
        let min: number = param.min;
        let max: number = param.max;
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    public Add(arg1: number, arg2: number): number {
        var arg1Arr = arg1.toString().split("."),
            arg2Arr = arg2.toString().split("."),
            d1 = arg1Arr.length == 2 ? arg1Arr[1] : "",
            d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
        var maxLen = Math.max(d1.length, d2.length);
        var m = Math.pow(10, maxLen);
        var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
        var d = arguments[2];
        return typeof d === "number" ? Number(result.toFixed(d)) : result;
    }
    public Sun(arg1: number, arg2: number): number {
        return this.Add(arg1, -arg2);
    }
    public Mul(arg1: number, arg2: number): number {
        var r1 = arg1.toString(),
            r2 = arg2.toString(),
            m: number,
            resultVal: number,
            d = arguments[2];
        m = (r1.split(".")[1] ? r1.split(".")[1].length : 0) + (r2.split(".")[1] ? r2.split(".")[1].length : 0);
        resultVal = (Number(r1.replace(".", "")) * Number(r2.replace(".", ""))) / Math.pow(10, m);
        return resultVal;
    }
    public Div(arg1: number, arg2: number): number {
        var r1 = arg1.toString(),
            r2 = arg2.toString(),
            m: number,
            resultVal: number;
        m = (r2.split(".")[1] ? r2.split(".")[1].length : 0) - (r1.split(".")[1] ? r1.split(".")[1].length : 0);
        resultVal = (Number(r1.replace(".", "")) / Number(r2.replace(".", ""))) * Math.pow(10, m);
        return resultVal;
    }
}

class ScriptTool_calculator implements IScriptTool<ICalculator> {
    GetCase(): ICalculator {
        return new calculator_nomal();
    }
}
export default ScriptTool_calculator;
